<template>
  <div class="app-container">
    <el-button class="create" type="primary" @click="create">创建</el-button>

    <el-table :data="list" style="width: 100%">
      <el-table-column prop="id" label="ID" width="180"> </el-table-column>
      <el-table-column prop="rackCode" label="鞋架编码"> </el-table-column>
      <el-table-column label="鞋架行列">
        <template slot-scope="scope">
          <span>{{ scope.row.rowNum }} - {{ scope.row.colNum }}</span>
        </template>
      </el-table-column>
      <el-table-column prop="useNum" label="使用数量"> </el-table-column>
      <el-table-column prop="status" label="状态">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.status == 1" type="success">上架</el-tag>
          <el-tag v-if="scope.row.status == 2" type="danger">下架</el-tag>
        </template>
      </el-table-column>
      <el-table-column prop="remark" label="备注"> </el-table-column>
      <el-table-column prop="updateBy" label="操作人"> </el-table-column>
      <el-table-column prop="updateTime" label="更新时间" width="180">
      </el-table-column>
      <el-table-column label="操作" width="180">
        <template slot-scope="scope">
          <el-button type="text" size="small" @click="edit(scope.row.id)"
            >编辑</el-button
          >
          <el-button
            type="text"
            size="small"
            v-if="scope.row.status == 2"
            @click="updateStatus(scope.row, '1')"
            >上架</el-button
          >
          <el-button
            type="text"
            size="small"
            v-if="scope.row.status == 1"
            @click="updateStatus(scope.row, '2')"
            >下架</el-button
          >

          <el-popconfirm
            style="margin-left: 10px"
            title="您确定要删除当前鞋架吗？"
            @confirm="deleteItem(scope.row.id)"
          >
            <el-button type="text" size="small" slot="reference"
              >删除</el-button
            >
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="queryParams.total > 0"
      :total="queryParams.total"
      :page.sync="queryParams.page"
      :limit.sync="queryParams.pageSize"
      @pagination="getListData"
    />
  </div>
</template>

<script>
import {
  getShoeRackList,
  addOrEditShoeRack,
  deleteShoeRack,
} from "@/api/shoeRack/index";
import { codeToText } from "element-china-area-data";
export default {
  name: "Index",
  data() {
    return {
      queryParams: {
        page: 0,
        pageSize: 10,
        total: 0,
      },
      list: [],
      codeToText,
    };
  },
  created() {
    this.getListData({
      page: 1,
      limit: 10,
    });
  },
  methods: {
    getListData({ page, limit }) {
      getShoeRackList(
        this.queryParams,
        page || this.queryParams.page + 1,
        limit
      ).then(({ data }) => {
        this.list = data.records;
        this.queryParams.total = data.total;
        this.queryParams.page = data.current;
        this.queryParams.limit = data.size;
      });
    },
    updateStatus(item, status) {
      addOrEditShoeRack({
        ...item,
        status,
      }).then((res) => {
        this.loading = false;
        this.$message.success("操作成功");
        this.getListData(this.queryParams);
      });
    },
    deleteItem(id) {
      deleteShoeRack(id).then((res) => {
        this.$message.success("删除成功");
        this.getListData(this.queryParams);
      });
    },
    create() {
      this.$router.push({ path: "/shoeRack/edit" });
    },
    edit(id) {
      // console.log(id, "zxcasd");
      this.$router.push({ path: `/shoeRack/edit?id=${id}` });
    },
  },
};
</script>

<style lang="scss" scoped>
.create {
  margin-bottom: 20px;
}
</style>
